package com.yili.oa.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.yili.oa.utils.hodle.DynamicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/10 0010
 * @email 200890949@qq.com
 * @remark 数据库多数据源配置
 */
@Configuration
public class DataSourceConfig{

    @Value("${spring.dbs.lyt_game_db.username}")
    private String lytGameDbUsername;

    @Value("${spring.dbs.lyt_game_db.password}")
    private String lytGameDbPassword;

    @Value("${spring.dbs.lyt_game_db.url}")
    private String lytGameDbUrl;

    @Value("${spring.dbs.lyt_game_db.driver-class-name}")
    private String lytGameDbDriverClassName;

    @Value("${spring.dbs.db_manage.username}")
    private String DbManageUsername;

    @Value("${spring.dbs.db_manage.password}")
    private String DbManagePassword;

    @Value("${spring.dbs.db_manage.url}")
    private String DbManageUrl;

    @Value("${spring.dbs.db_manage.driver-class-name}")
    private String DbManageDriverClassName;

/*    @Value("${spring.dbs.db_dealers.username}")
    private String DbDealersUsername;

    @Value("${spring.dbs.db_dealers.password}")
    private String DbDealersPassword;

    @Value("${spring.dbs.db_dealers.url}")
    private String DbDealersUrl;

    @Value("${spring.dbs.db_dealers.driver-class-name}")
    private String lDbDealersDriverClassName;*/


    @Autowired
    private DynamicDataSource dataSource;



    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        try {
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    /**
     * db1数据库配置
     */
    @Bean("db_manage")
    public DataSource db1Source() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(DbManageDriverClassName);
        dataSource.setUrl(DbManageUrl);
        dataSource.setUsername(DbManageUsername );
        dataSource.setPassword(DbManagePassword);
        return dataSource;
    }

    /**
     * db2数据库配置
     */
/*    @Bean("db_dealers")
     public DataSource db2Source() {

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(lDbDealersDriverClassName);
        dataSource.setUrl(DbDealersUrl);
        dataSource.setUsername(DbDealersUsername);
        dataSource.setPassword( DbDealersPassword);
        return dataSource;
    }*/

    /**
     * db2数据库配置
     */
    @Bean("lyt_game_db")
    public DataSource db3Source() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(lytGameDbDriverClassName);
        dataSource.setUrl(lytGameDbUrl);
        dataSource.setUsername(lytGameDbUsername);
        dataSource.setPassword(lytGameDbPassword);
        return dataSource;
    }

    /**
     * 动态数据库配置
     */
    @Primary
    @Bean(name = "dynamicDataSource")
    public DynamicDataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(db1Source());

        // 配置多数据源
        Map<Object, Object> dsMap = new HashMap(5);
        dsMap.put("db_manage", db1Source());
//        dsMap.put("db_dealers", db2Source());
        dsMap.put("lyt_game_db", db3Source());

        dynamicDataSource.setTargetDataSources(dsMap);

        return dynamicDataSource;
    }

    /**
     * 配置@Transactional注解事物
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }

    
}